home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / Platform.java < prev    next >
Text File  |  1998-10-14  |  17KB  |  678 lines

  1. package com.symantec.itools.lang;
  2.  
  3.  
  4. import java.io.IOException;
  5. import com.symantec.itools.util.Properties;
  6.  
  7.  
  8. /**
  9.  * The Platform class abstracts varous System properties to provide
  10.  * a consistant set of names.  Different vendors provide different
  11.  * values for these System properties which makes it hard to do
  12.  * comparisons against them.
  13.  *
  14.  * The following properties are abstracted:
  15.  *    - java.vendor
  16.  *    - java.version
  17.  *    - os.name
  18.  *    - os.version
  19.  *    - os.arch
  20.  *
  21.  * The concept of an OS family (eg: Windows NT, Window 95 and
  22.  * Windows 98 are all part of the Windows family) is also
  23.  * introduced.
  24.  *
  25.  * The abstraction is driven by a Symantec style Property file.
  26.  * This file is broken up into three main sections:
  27.  *
  28.  *    - vm; abstracts the VM Info
  29.  *    - os; abstracts the OS info
  30.  *    - machine; abstracts the Architecture info
  31.  *
  32.  * Each section is further broken down by Java vendor.  The
  33.  * reasoning behind choosing the Java vendor is that it makes
  34.  * for finer control over the information.  If no vendor were
  35.  * used the number of conflicting values and thus the chance
  36.  * for error gets quite large.  Trying to combine similar VMs
  37.  * (Symantec and Sun return the same values currently) makes
  38.  * the file too complicated.
  39.  *
  40.  * The Property file is /com/symantec/itools/lang/Platform.properties
  41.  * Any unknown values can easily be added to the property file
  42.  * without having to recompile any code.
  43.  *
  44.  * @author Symantec Internet Tools Division
  45.  * @version 1.0
  46.  * @since VCafe 3.0
  47.  * @see com.symantec.itools.util.Properties
  48.  */
  49.  
  50. public class Platform
  51. {
  52.     /**
  53.      * Tell if unknown information should be reported
  54.      * @since VCafe 3.0
  55.      */
  56.     protected static boolean reportErrors;
  57.  
  58.     /**
  59.      * The key from the property file; null if unknown
  60.      * @since VCafe 3.0
  61.      */
  62.     protected static String javaVendor;
  63.  
  64.     /**
  65.      * The key from the property file; null if unknown
  66.      * @since VCafe 3.0
  67.      */
  68.     protected static String javaVersion;
  69.  
  70.     /**
  71.      * The key from the property file; null if unknown.
  72.      * @since VCafe 3.0
  73.      */
  74.     protected static String osName;
  75.  
  76.     /**
  77.      * The key from the property file; null if unknown
  78.      * @since VCafe 3.0
  79.      */
  80.     protected static String osVersion;
  81.  
  82.     /**
  83.      * The key from the property file; null if unknown
  84.      * @since VCafe 3.0
  85.      */
  86.     protected static String osType;
  87.  
  88.     /**
  89.      * The key from the property file; null if unknown
  90.      * @since VCafe 3.0
  91.      */
  92.     protected static String architecture;
  93.  
  94.     /**
  95.      * Holds the info from the property file.
  96.      * @since VCafe 3.0
  97.      */
  98.     protected static Properties properties;
  99.  
  100.     /**
  101.      * Constant representing Apple.
  102.      * @since VCafe 3.0
  103.      * @see #javaVendor
  104.      * @see #isJavaVendor
  105.      */
  106.     public static final String VENDOR_APPLE = "Apple";
  107.  
  108.     /**
  109.      * Constant representing IBM.
  110.      * @since VCafe 3.0
  111.      * @see #javaVendor
  112.      * @see #isJavaVendor
  113.      */
  114.     public static final String VENDOR_IBM = "IBM";
  115.  
  116.     /**
  117.      * Constant representing Microsoft.
  118.      * @since VCafe 3.0
  119.      * @see #javaVendor
  120.      * @see #isJavaVendor
  121.      */
  122.     public static final String VENDOR_MICROSOFT = "Microsoft";
  123.  
  124.     /**
  125.      * Constant representing Netscape.
  126.      * @since VCafe 3.0
  127.      * @see #javaVendor
  128.      * @see #isJavaVendor
  129.      */
  130.     public static final String VENDOR_NETSCAPE = "Netscape";
  131.  
  132.     /**
  133.      * Constant representing Silicon Graphics.
  134.      * @since VCafe 3.0
  135.      * @see #javaVendor
  136.      * @see #isJavaVendor
  137.      */
  138.     public static final String VENDOR_SGI = "SGI";
  139.  
  140.     /**
  141.      * Constant representing Sun.
  142.      * @since VCafe 3.0
  143.      * @see #javaVendor
  144.      * @see #isJavaVendor
  145.      */
  146.     public static final String VENDOR_SUN = "Sun";
  147.  
  148.     /**
  149.      * Constant representing Symantec.
  150.      * @since VCafe 3.0
  151.      * @see #javaVendor
  152.      * @see #isJavaVendor
  153.      */
  154.     public static final String VENDOR_SYMANTEC = "Symantec";
  155.  
  156.     /**
  157.      * Constant representing Digital.
  158.      * @since VCafe 3.0
  159.      * @see #javaVendor
  160.      * @see #isJavaVendor
  161.      */
  162.     public static final String VENDOR_DEC = "DEC";
  163.  
  164.     /**
  165.      * Constant representing Hewlet Packard.
  166.      * @since VCafe 3.0
  167.      * @see #javaVendor
  168.      * @see #isJavaVendor
  169.      */
  170.     public static final String VENDOR_HP = "HP";
  171.  
  172.     /**
  173.      * Constant representing the Linux JDK port.
  174.      * @since VCafe 3.0
  175.      * @see #javaVendor
  176.      * @see #isJavaVendor
  177.      */
  178.     public static final String VENDOR_LINUX_PORT = "LinuxPort";
  179.  
  180.     /**
  181.      * @since VCafe 3.0
  182.      */
  183.     public static final String JDK_1_1 = "1-1";
  184.  
  185.     /**
  186.      * @since VCafe 3.0
  187.      */
  188.     public static final String JDK_1_2 = "1-2";
  189.  
  190.     /**
  191.      * @since VCafe 3.0
  192.      */
  193.     public static final String OS_WINDOWS_NT = "WindowsNT";
  194.  
  195.     /**
  196.      * @since VCafe 3.0
  197.      */
  198.     public static final String OS_WINDOWS_95 = "Windows95";
  199.  
  200.     /**
  201.      * @since VCafe 3.0
  202.      */
  203.     public static final String OS_WINDOWS_98 = "Windows98";
  204.  
  205.     /**
  206.      * @since VCafe 3.0
  207.      */
  208.     public static final String OS_MACINTOSH = "Macintosh";
  209.  
  210.     /**
  211.      * @since VCafe 3.0
  212.      */
  213.     public static final String OS_SOLARIS = "Solaris";
  214.  
  215.     /**
  216.      * @since VCafe 3.0
  217.      */
  218.     public static final String OS_IRIX = "Irix";
  219.  
  220.     /**
  221.      * @since VCafe 3.0
  222.      */
  223.     public static final String OS_DIGITAL_UNIX = "DigitalUnix";
  224.  
  225.     /**
  226.      * @since VCafe 3.0
  227.      */
  228.     public static final String OS_AIX = "AIX";
  229.  
  230.     /**
  231.      * @since VCafe 3.0
  232.      */
  233.     public static final String OS_HPUX = "HPUX";
  234.  
  235.     /**
  236.      * @since VCafe 3.0
  237.      */
  238.     public static final String OS_LINUX = "Linux";
  239.  
  240.     /**
  241.      * @since VCafe 3.0
  242.      */
  243.     public static final String OS_TYPE_MAC_OS = "MacOS";
  244.  
  245.     /**
  246.      * @since VCafe 3.0
  247.      */
  248.     public static final String OS_TYPE_WINDOWS = "Windows";
  249.  
  250.     /**
  251.      * @since VCafe 3.0
  252.      */
  253.     public static final String OS_TYPE_UNIX = "Unix";
  254.  
  255.     /**
  256.      * @since VCafe 3.0
  257.      */
  258.     public static final String ARCHITECTURE_MIPS = "MIPS";
  259.  
  260.     /**
  261.      * @since VCafe 3.0
  262.      */
  263.     public static final String ARCHITECTURE_X86 = "x86";
  264.  
  265.     /**
  266.      * @since VCafe 3.0
  267.      */
  268.     public static final String ARCHITECTURE_ALPHA = "ALPHA";
  269.  
  270.     /**
  271.      * @since VCafe 3.0
  272.      */
  273.     public static final String ARCHITECTURE_SPARC = "SPARC";
  274.  
  275.     /**
  276.      * @since VCafe 3.0
  277.      */
  278.     public static final String ARCHITECTURE_PA_RISC = "PA-RISC";
  279.  
  280.     static
  281.     {
  282.         try
  283.         {
  284.             properties = new Properties("/com/symantec/itools/lang/Platform.properties");
  285.         }
  286.         catch(Throwable ex)
  287.         {
  288.             ex.printStackTrace();
  289.         }
  290.     }
  291.  
  292.     static
  293.     {
  294.         reportErrors = true;
  295.     }
  296.  
  297.     static
  298.     {
  299.         try
  300.         {
  301.             Properties info;
  302.  
  303.             info = properties.getProperties("vm");
  304.  
  305.             if(info != null)
  306.             {
  307.                 javaVendor  = lookupJavaVendor(info);
  308.  
  309.                 if(javaVendor != null)
  310.                 {
  311.                     javaVersion = lookupJavaVersion(info);
  312.                     info        = properties.getProperties("os");
  313.  
  314.                     if(info != null)
  315.                     {
  316.                         osName = lookupOSName(info);
  317.  
  318.                         if(osName != null)
  319.                         {
  320.                             osVersion = lookupOSVersion(info);
  321.                             osType    = lookupOSType(info);
  322.                         }
  323.                     }
  324.  
  325.                     info = properties.getProperties("machine");
  326.  
  327.                     if(info != null)
  328.                     {
  329.                         architecture = lookupArchitecture(info);
  330.                     }
  331.                 }
  332.             }
  333.         }
  334.         catch(Throwable ex)
  335.         {
  336.             ex.printStackTrace();
  337.         }
  338.  
  339.         fixupOSName();
  340.  
  341.         if(reportErrors)
  342.         {
  343.             reportErrorFor("java.vendor", javaVendor, true);
  344.             reportErrorFor("java.version", javaVersion, true);
  345.             reportErrorFor("os.name", osName, true);
  346.             reportErrorFor("os.version", osVersion, true);
  347.             reportErrorFor("the OS type", osType, false);
  348.             reportErrorFor("os.arch", architecture, true);
  349.         }
  350.     }
  351.  
  352.  
  353.     /**
  354.      * @since VCafe 3.0
  355.      */
  356.     protected Platform()
  357.     {
  358.         throw new IllegalInstantiationError(Platform.class);
  359.     }
  360.  
  361.     /**
  362.      * @param vendor The representation of the Java vendor.  This is the key in the properties file.
  363.      * @since VCafe 3.0
  364.      */
  365.     public static boolean isJavaVendor(String vendor)
  366.     {
  367.         return (vendor.equals(javaVendor));
  368.     }
  369.  
  370.     /**
  371.      * @param version The representation of the Java version.  This is the key in the properties file.
  372.      * @since VCafe 3.0
  373.      */
  374.     public static boolean isJavaVersion(String version)
  375.     {
  376.         return (version.equals(javaVersion));
  377.     }
  378.  
  379.     /**
  380.      * @param type The representation of the OS family.  This is the key in the properties file.
  381.      * @since VCafe 3.0
  382.      */
  383.     public static boolean isOSType(String type)
  384.     {
  385.         return (type.equals(osType));
  386.     }
  387.  
  388.     /**
  389.      * @param os The representation of the OS name.  This is the key in the properties file.
  390.      * @since VCafe 3.0
  391.      */
  392.     public static boolean isOSName(String os)
  393.     {
  394.         return (os.equals(osName));
  395.     }
  396.  
  397.     /**
  398.      * @param arch The representation of the OS version.  This is the key in the properties file.
  399.      * @since VCafe 3.0
  400.      */
  401.     public static boolean isArchitecture(String arch)
  402.     {
  403.         return (arch.equals(architecture));
  404.     }
  405.  
  406.     /**
  407.      * @param vmInfo The properties object that holds the "vm" part of the properties file.
  408.      * @since VCafe 3.0
  409.      */
  410.     private static String lookupJavaVendor(Properties vmInfo)
  411.     {
  412.         final String prop = System.getProperty("java.vendor").trim();
  413.  
  414.         Properties vendorInfo;
  415.         String[]   vendorList;
  416.  
  417.         vendorInfo = vmInfo.getProperties("vendors");
  418.  
  419.         if(vendorInfo == null)
  420.         {
  421.             throw new RuntimeException("Need a better error - vendorInfo is null");
  422.         }
  423.  
  424.         vendorList = vendorInfo.getStringList("vendors");
  425.  
  426.         for(int i = 0; i < vendorList.length; i++)
  427.         {
  428.             String[] list;
  429.  
  430.             list = vendorInfo.getStringList(vendorList[i]);
  431.  
  432.             for(int j = 0; j < list.length; j++)
  433.             {
  434.                 if(prop.equals(list[j]))
  435.                 {
  436.                     return (vendorList[i]);
  437.                 }
  438.             }
  439.         }
  440.  
  441.         return (null);
  442.     }
  443.  
  444.     /**
  445.      * @param vmInfo The properties object that holds the "vm" part of the properties file.
  446.      * @since VCafe 3.0
  447.      */
  448.     private static String lookupJavaVersion(Properties vmInfo)
  449.     {
  450.         final String prop = System.getProperty("java.version").trim();
  451.  
  452.         Properties versionInfo;
  453.         String[]   versionList;
  454.  
  455.         versionInfo = vmInfo.getProperties("versions");
  456.  
  457.         if(versionInfo == null)
  458.         {
  459.             throw new RuntimeException("Need a better error - versionInfo is null");
  460.         }
  461.  
  462.         versionList = versionInfo.getStringList("versions");
  463.  
  464.         for(int i = 0; i < versionList.length; i++)
  465.         {
  466.             String[] list;
  467.  
  468.             list = versionInfo.getStringList(versionList[i] + '.' + javaVendor);
  469.  
  470.             for(int j = 0; j < list.length; j++)
  471.             {
  472.                 if(prop.equals(list[j]))
  473.                 {
  474.                     return (versionList[i]);
  475.                 }
  476.             }
  477.         }
  478.  
  479.         return (null);
  480.     }
  481.  
  482.     /**
  483.      * @param osInfo The properties object that holds the "os" part of the properties file.
  484.      * @since VCafe 3.0
  485.      */
  486.     private static String lookupOSName(Properties osInfo)
  487.     {
  488.         final String prop = System.getProperty("os.name").trim();
  489.  
  490.         Properties nameInfo;
  491.         String[]   nameList;
  492.  
  493.         nameInfo = osInfo.getProperties("names");
  494.  
  495.         if(nameInfo == null)
  496.         {
  497.             throw new RuntimeException("Need a better error - nameInfo is null");
  498.         }
  499.  
  500.         nameList = nameInfo.getStringList("names");
  501.  
  502.         for(int i = 0; i < nameList.length; i++)
  503.         {
  504.             String[] list;
  505.  
  506.             list = nameInfo.getStringList(nameList[i] + '.' + javaVendor);
  507.  
  508.             for(int j = 0; j < list.length; j++)
  509.             {
  510.                 if(prop.equals(list[j]))
  511.                 {
  512.                     return (nameList[i]);
  513.                 }
  514.             }
  515.         }
  516.  
  517.         return (null);
  518.     }
  519.  
  520.     /**
  521.      * @param osInfo The properties object that holds the "os" part of the properties file.
  522.      * @since VCafe 3.0
  523.      */
  524.     private static String lookupOSVersion(Properties osInfo)
  525.     {
  526.         final String prop = System.getProperty("os.version").trim();
  527.  
  528.         Properties versionInfo;
  529.         String[]   versionList;
  530.  
  531.         versionInfo = osInfo.getProperties("versions");
  532.  
  533.         if(versionInfo == null)
  534.         {
  535.             throw new RuntimeException("Need a better error - versionInfo is null");
  536.         }
  537.  
  538.         versionList = versionInfo.getStringList(osName);
  539.  
  540.         for(int i = 0; i < versionList.length; i++)
  541.         {
  542.             String[] list;
  543.  
  544.             list = versionInfo.getStringList(osName + '.' + versionList[i] + '.' + javaVendor);
  545.  
  546.             for(int j = 0; j < list.length; j++)
  547.             {
  548.                 if(prop.equals(list[j]))
  549.                 {
  550.                     return (versionList[i]);
  551.                 }
  552.             }
  553.         }
  554.  
  555.         return (null);
  556.     }
  557.  
  558.     /**
  559.      * @param osInfo The properties object that holds the "os" part of the properties file.
  560.      * @since VCafe 3.0
  561.      */
  562.     private static String lookupOSType(Properties osInfo)
  563.     {
  564.         Properties typeInfo;
  565.         String[]   typeList;
  566.  
  567.         typeInfo = osInfo.getProperties("types");
  568.  
  569.         if(typeInfo == null)
  570.         {
  571.             throw new RuntimeException("Need a better error - typesInfo is null");
  572.         }
  573.  
  574.         typeList = typeInfo.getStringList("types");
  575.  
  576.         for(int i = 0; i < typeList.length; i++)
  577.         {
  578.             String[] list;
  579.  
  580.             list = typeInfo.getStringList(typeList[i]);
  581.  
  582.             for(int j = 0; j < list.length; j++)
  583.             {
  584.                 if(osName.equals(list[j]))
  585.                 {
  586.                     return (typeList[i]);
  587.                 }
  588.             }
  589.         }
  590.  
  591.         return (null);
  592.     }
  593.  
  594.     /**
  595.      * @param machineInfo The properties object that holds the "machine" part of the properties file.
  596.      * @since VCafe 3.0
  597.      */
  598.     private static String lookupArchitecture(Properties machineInfo)
  599.     {
  600.         final String prop = System.getProperty("os.arch").trim();
  601.  
  602.         String[] architectureList;
  603.  
  604.         architectureList = machineInfo.getStringList("machines");
  605.  
  606.         for(int i = 0; i < architectureList.length; i++)
  607.         {
  608.             String[] list;
  609.  
  610.             list = machineInfo.getStringList(architectureList[i] + '.' + javaVendor);
  611.  
  612.             for(int j = 0; j < list.length; j++)
  613.             {
  614.                 if(prop.equals(list[j]))
  615.                 {
  616.                     return (architectureList[i]);
  617.                 }
  618.             }
  619.         }
  620.  
  621.         return (null);
  622.     }
  623.  
  624.     /**
  625.      * @param key The key for the System property
  626.      * @param value The value from the property file; if null an error is reported
  627.      * @param showSysProp TODO
  628.      * @since VCafe 3.0
  629.      */
  630.     private static void reportErrorFor(String key, String value, boolean showSysProp)
  631.     {
  632.         if(value == null)
  633.         {
  634.             System.out.print("Platform doesn't know about " + key);
  635.  
  636.             if(showSysProp)
  637.             {
  638.                 System.out.print(": " + System.getProperty(key));
  639.             }
  640.  
  641.             System.out.println();
  642.         }
  643.     }
  644.  
  645.     /**
  646.      * Gives the chance to modify the name of the OS once all of the
  647.      * information is known.  For example Win98 reports an OS name of
  648.      * Win95 - but the version numbers differ.  If the name were not
  649.      * fixed up the check for Win98 would have to be to look at the
  650.      * OS name and the OS version.
  651.      * @since VCafe 3.0
  652.      */
  653.     private static void fixupOSName()
  654.     {
  655.         if(osName == null | osVersion == null)
  656.         {
  657.             return;
  658.         }
  659.  
  660.         // handle the screwed up MS OS naming convention where Win98 reports as Win95!
  661.         if(osName.equals(OS_WINDOWS_95))
  662.         {
  663.             if(osVersion.equals("4.10"))
  664.             {
  665.                 osName = OS_WINDOWS_98;
  666.             }
  667.         }
  668.     }
  669.  
  670.     /**
  671.      * This exists for testing purposes only.
  672.      * @param argv Not used
  673.      * @since VCafe 3.0
  674.      */
  675.     public static void main(String[] argv)
  676.     {
  677.     }
  678. }